#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifi_device.h"

#include "lwip/netifapi.h"
#include "lwip/api_shell.h"

#define __SSID__   "TEST"
#define __PASSWD__ "12345678"

static void PrintLinkedInfo(WifiLinkedInfo* info)
{
    if (!info) return;

    static char macAddress[32] = {0};
    unsigned char* mac = info->bssid;
    snprintf(macAddress, sizeof(macAddress), "%02X:%02X:%02X:%02X:%02X:%02X",
        mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
    printf("bssid: %s, rssi: %d, connState: %d, reason: %d, ssid: %s\r\n",
        macAddress, info->rssi, info->connState, info->disconnectedReason, info->ssid);
}

static int g_connected = 0;
static void OnWifiConnectionChanged(int state, WifiLinkedInfo* info)
{
    if (!info) return;

    printf("%s %d, state = %d, info = \r\n", __FUNCTION__, __LINE__, state);
    PrintLinkedInfo(info);

    //3.2更高的版本，拼写是正确，低版本是错 WIFI_STATE_AVALIABLE
    if (state == WIFI_STATE_AVALIABLE) {
        g_connected = 1;
    } else {
        g_connected = 0;
    }
}

static void OnWifiScanStateChanged(int state, int size)
{
    printf("%s %d, state = %X, size = %d\r\n", __FUNCTION__, __LINE__, state, size);
    /*  把所有扫描到的WiFi热点打印 */
}

static void __WifiConnect(void)
{
    WifiErrorCode errCode;

    //监听
    WifiEvent eventListener = {
        //链接状态发生变化  连上了 / 断开
        .OnWifiConnectionChanged = OnWifiConnectionChanged,

        //扫描结果发生变化，
        .OnWifiScanStateChanged = OnWifiScanStateChanged
    };
    
    

    osDelay(10);
    //注册监听事件
    errCode = RegisterWifiEvent(&eventListener);
    printf("RegisterWifiEvent: %d\r\n", errCode);

    //得有配置
    WifiDeviceConfig apConfig = {};
    int netId = -1;
    
    // setup your AP params
    strcpy(apConfig.ssid, __SSID__);
    strcpy(apConfig.preSharedKey, __PASSWD__);
    apConfig.securityType = WIFI_SEC_TYPE_PSK;


        //打开STA模式
        errCode = EnableWifi();
        printf("EnableWifi: %d\r\n", errCode);
        osDelay(10);

        //把刚才得配置信息，传进去apConfig
        errCode = AddDeviceConfig(&apConfig, &netId);   //netId = 0
        printf("AddDeviceConfig: %d\r\n", errCode);

        //连接状态等于0
        g_connected = 0;
        
        //连接到热点
        errCode = ConnectTo(netId);
        printf("ConnectTo(%d): %d\r\n", netId, errCode);

        while (!g_connected) {
            osDelay(10);
        }
        printf("g_connected: %d\r\n", g_connected);
        osDelay(50);

        // 获取网络接口
        struct netif* iface = netifapi_netif_find("wlan0");
        if (iface) {
            //启动DHCP
            err_t ret = netifapi_dhcp_start(iface);
            printf("netifapi_dhcp_start: %d\r\n", ret);

            osDelay(200); // wait DHCP server give me IP
            
            //打印分配到的IP
            ret = netifapi_netif_common(iface, dhcp_clients_info_show, NULL);
            printf("netifapi_netif_common: %d\r\n", ret);
        }

}



void mqtt_test_thread(void * argv)
{

    argv = argv;

    //连接路由器
    __WifiConnect();

    sleep(3);
    car_test();

}


void StaExampleEntry(void)
{
    osThreadAttr_t attr;

    attr.name = "wifi_config_thread";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 4096;
    attr.priority = 36;

    if (osThreadNew((osThreadFunc_t)mqtt_test_thread, NULL, &attr) == NULL) {
        printf("[LedExample] Falied to create LedTask!\n");
    }
}

SYS_RUN(StaExampleEntry);